package com.nmc.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.commons.AjaxHelper;
import com.metaparadigm.jsonrpc.JSONRPCBridge;
import com.nmc.core.manager.TrackManager;
import com.nmc.core.manager.WardManager;
import com.nmc.core.manager.ZoneManager;
import com.nmc.model.Track;
import com.nmc.model.Ward;
import com.opensymphony.xwork2.ActionSupport;

public class TrackAction extends ActionSupport implements ServletRequestAware {

	private TrackManager trackManager;
	private Track track;
	HttpServletRequest request;
	private boolean resultPage;
	private List zoneList;
	private List<Ward> wardList = new ArrayList<Ward>();
	private ZoneManager zoneManager;
	private WardManager wardManager;
	private List<Track> trackList = new ArrayList<Track>();
    private String id;
	
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String createTrack(){
		JSONRPCBridge json_bridge = null;
		json_bridge = (JSONRPCBridge) request.getSession().getAttribute("JSONRPCBridge");
		if (json_bridge == null) {
			json_bridge = new JSONRPCBridge();
			request.getSession().setAttribute("JSONRPCBridge", json_bridge);
		}
		json_bridge.registerObject("getZoneList", new AjaxHelper(request.getSession().getServletContext()));
		
		System.out.println("GetTrack Id :"+id +" if "+(id != null && id.length()>0));
		if(id != null && id.length() >0){
			track = trackManager.getTrack(Integer.parseInt(id));
			System.out.println("Track "+track.getSweeperName());
		}
		return this.SUCCESS;
	}

	public String addTrack()
	{
		//System.out.println("addtrack");
		
		trackManager.createTrack(track);
		return this.SUCCESS;
	}
	public String admin()
	{
		System.out.println("admin");
		SecurityContext secContext = SecurityContextHolder.getContext();
		if(secContext != null)
		{
			if( secContext.getAuthentication() != null &&
					secContext.getAuthentication().isAuthenticated()){
			return "home";
			}
		}
		return this.SUCCESS;
	}
	public String viewTrack()
	{
		//trackList = trackManager.getTrackList();
		return this.SUCCESS;
	}

	public String deleteTrack()
	{
		trackManager.deleteTrack(track);
		trackList = trackManager.filterTrack(track);
		return this.SUCCESS;
	}
	public String filterTracks(){
		trackList = trackManager.filterTrack(track);
		//System.out.println("viewTrack "+trackList);
		return this.SUCCESS;
	}
	/**
	 * @param trackManager the trackManager to set
	 */
	public void setTrackManager(TrackManager trackManager) {
		this.trackManager = trackManager;
	}

	/**
	 * @return the trackManager
	 */
	public TrackManager getTrackManager() {
		return trackManager;
	}


	/**
	 * @param track the track to set
	 */
	public void setTrack(Track track) {
		this.track = track;
	}

	/**
	 * @return the track
	 */
	public Track getTrack() {
		
		return track;
	}

	/**
	 * @param zoneList the zoneList to set
	 */
	public void setZoneList(List zoneList) {
		this.zoneList = zoneList;
	}

	/**
	 * @return the zoneList
	 */
	public List getZoneList() {
		zoneList = getZoneManager().getZoneList();
		return zoneList;
	}

	/**
	 * @param zoneManager the zoneManager to set
	 */
	public void setZoneManager(ZoneManager zoneManager) {
		this.zoneManager = zoneManager;
	}

	/**
	 * @return the zoneManager
	 */
	public ZoneManager getZoneManager() {
		return zoneManager;
	}

	/**
	 * @param trackList the trackList to set
	 */
	public void setTrackList(List<Track> trackList) {
		this.trackList = trackList;
	}

	/**
	 * @return the trackList
	 */
	public List<Track> getTrackList() {
		return trackList;
	}

	/**
	 * @param wardList the wardList to set
	 */
	public void setWardList(List<Ward> wardList) {
		this.wardList = wardList;
	}

	/**
	 * @return the wardList
	 */
	public List<Ward> getWardList() {
		if(track!= null && track.getZoneId() >0){
			wardList = wardManager.getWardList(track.getZoneId());
		}
		return wardList;
	}

	/**
	 * @param wardManager the wardManager to set
	 */
	public void setWardManager(WardManager wardManager) {
		this.wardManager = wardManager;
	}

	/**
	 * @return the wardManager
	 */
	public WardManager getWardManager() {
		return wardManager;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	
	
}
